#include "gb.h"

#define LOCAL_HOST  "192.168.101.7"

#define GB_SERVER "192.168.101.230"

struct eXosip_t* eXosip;

void ServerRegisterSucessParse(void *pout, Uint32 outlen)
{

}

void CatalogParse(Uint32 user_id, const char *net_id, Uint32 exp_id, void *pout, Uint32 outlen)
{
	mk_catalog_t catalog;
	mk_catalog_item_t Item;
	Item.latitude = 0.0;
	Item.longitude = 0.0;
	strcpy(Item.manufacturer, "manufacturer");
	strcpy(Item.model, "Model");

	Item.online = 1;
	strcpy(Item.owner, "Owner");
	strcpy(Item.parent_id, "34020000001320000001");
	Item.parental = 0;
	Item.secrecy = 0;
	strcpy(Item.address, "Address");
	strcpy(Item.civil_code, "CivilCode");
	for (int i = 0; i < 10; i++)
	{
		snprintf(Item.name, LEN_256, "Channel %02d", i + 1);
		snprintf(Item.id, id_len, "340200000013200000%02d", i + 1);
		catalog.catalogList.push_back(Item);
	}
	gb_send_msg_ack(eXosip, user_id, net_id, query_catalog, &catalog, sizeof(catalog), exp_id);
}

void StartPlayParse(Uint32 user_id, const char *net_id, Uint32 exp_id, void *pout, Uint32 outlen)
{
	mk_play_request_param_t* playinfo = (mk_play_request_param_t*)pout;

	mk_play_response_info_t info = { 0 };
	info.play_id = playinfo->play_id;
	info.load_type = realplay;
	info.error_code = 200;
	strcpy(info.src_host, LOCAL_HOST);
	gb_send_msg_ack(eXosip, user_id, net_id, start_play, &info, sizeof(info), exp_id);
}

void StopPlayParse(Uint32 user_id, const char *net_id, Uint32 exp_id, void *pout, Uint32 outlen)
{

}

Uint32 ExceptionCallback(struct eXosip_t* ContentOsip, Uint32 user_id, const char *net_id, Uint32 exp_id, void *user, mk_gb_cmd_type cmd_type, void *pout, Uint32 outlen)
{
	switch (cmd_type)
	{
	case device_online:
		//DeviceOnineParse(user_id, net_id,pout,outlen);
		break;
	case device_auto_add:
		break;
	case device_offline:
		//DeviceOfflineParse(net_id,pout,outlen);
		break;
	case query_catalog:
		CatalogParse(user_id, net_id, exp_id, pout, outlen);
		break;
	case start_play:
		StartPlayParse(user_id, net_id, exp_id, pout, outlen);
		break;
	case stop_play:
		StopPlayParse(user_id, net_id, exp_id, pout, outlen);
		break;
	case register_success:
		ServerRegisterSucessParse(pout, outlen);
		break;
	default:
		break;
	}
	return NoneError;
}

int main(int argc, char** argv)
{
	Uint32 server_id = 0;
	gb_add_white_id("1", "34020000001320000002", "12");
	gb_set_device_access_count(10);
	mk_sip_server_init_param_t param;
	param.authorizate = 0;
	param.cb = ExceptionCallback;
	strcpy(param.local_id, "34020000001320000005");
	strcpy(param.host, LOCAL_HOST);
	param.port = 5060;
	param.transport = IPPROTO_UDP;
	param.user = NULL;
	param.wait_time = 3000;
	Uint32 ErrorCode = gb_init(&param, eXosip);
	if (NoneError != ErrorCode)
	{
		return ErrorCode;
	}
	mk_gb_server_param_t server;
	server.expries = 3600;
	server.hb_count = 3;
	server.hb_interval = 60;
	strcpy(server.host, GB_SERVER);
	strcpy(server.id, "34020000002000000001");
	server.port = 5060;
	strcpy(server.realm, "5103040000");
	strcpy(server.username, "34020000002000000005");
	strcpy(server.password, "12345678");
	server_id = gb_add_server(&server);
}